package com.example.kaogong.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.Student;
import com.example.kaogong.sys.pojo.Teacher;
import com.example.kaogong.sys.pojo.vo.StudentInfoVo;
import com.example.kaogong.sys.pojo.vo.TeacherInfoVo;
import com.example.kaogong.sys.pojo.vo.TeacherPostVo;
import com.example.kaogong.sys.service.IStudentService;
import com.example.kaogong.sys.service.ITeacherPostService;
import com.example.kaogong.sys.service.ITeacherService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @description: admin前端控制类
 * @author: 徐文升
 * @create: 2022-03-27 15:13
 **/

@RestController
@RequestMapping(value = "/admin")
@Api(tags = "管理员接口")
@Slf4j
public class AdminController {

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private ITeacherPostService teacherPostService;

    @GetMapping(value = "/manage/teacher/{current}/{size}")
    @ApiOperation(value = "分页获取所有教师信息", notes = "current-当前第几页， size-每页大小")
    public RespBean<Page<TeacherInfoVo>> getAllTeacherInfo(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<TeacherInfoVo>> respBean = teacherService.getAllTeacherInfo(current, size);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @GetMapping(value = "/manage/student/{current}/{size}")
    @ApiOperation(value = "分页获取所有学生信息", notes = "current-当前第几页， size-每页大小")
    public RespBean<Page<StudentInfoVo>> getAllStudentInfo(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<StudentInfoVo>> respBean = studentService.getAllStudentInfo(current, size);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @DeleteMapping(value = "/manage/student/{sid}")
    @ApiOperation(value = "根据学生编号删除一条学生信息")
    public RespBean deleteStudentBySid(@PathVariable Integer sid) {
        Assert.notNull(sid, new ParamsErrorException("学生编号不能为空"));
        log.info("sid:{}", sid);
        RespBean respBean = studentService.deleteStudentBySid(sid);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @PutMapping(value = "/manage/student")
    @ApiOperation(value = "修改学生信息")
    public RespBean updateStudentInfo(@Valid @RequestBody Student student) {
        log.info("sid:{}, request:{}", student.getSid(), new Gson().toJson(student));
        RespBean respBean = studentService.updateStudentInfo(student);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @PutMapping(value = "/manage/teacher")
    @ApiOperation(value = "修改教师信息")
    public RespBean updateTeacherInfo(@Valid @RequestBody Teacher teacher) {
        log.info("tid:{}, request:{}", teacher.getTeacherId(), new Gson().toJson(teacher));
        RespBean respBean = teacherService.updateTeacherInfo(teacher);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @GetMapping(value = "/manage/teacher/post")
    @ApiOperation(value = "获取所有教师职称信息")
    public RespBean<List<TeacherPostVo>> getAllTeacherPost() {
        RespBean<List<TeacherPostVo>> respBean = teacherPostService.getAllTeacherPost();
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }


    @DeleteMapping(value = "/manage/teacher/{tid}")
    @ApiOperation(value = "根据教师编号删除一条教师信息")
    public RespBean deleteTeacherByTid(@PathVariable Integer tid) {
        Assert.notNull(tid, new ParamsErrorException("教师编号不能为空"));
        log.info("tid:{}", tid);
        RespBean respBean = teacherService.deleteTeacherByTid(tid);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @PostMapping(value = "/manage/post/{postName}")
    @ApiOperation(value = "新增一条教师职称")
    public RespBean addTeacherPost(@PathVariable String postName) {
        Assert.notBlank(postName, new ParamsErrorException("职称不能为空"));
        log.info("postName:{}", postName);
        RespBean respBean = teacherPostService.addTeacherPost(postName);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @DeleteMapping(value = "/manage/post/{postId}")
    @ApiOperation(value = "根据职称编号删除职称")
    public RespBean deleteTeacherPost(@PathVariable Integer postId) {
        Assert.notNull(postId, new ParamsErrorException("职称编号不能为空"));
        log.info("postId:{}", postId);
        RespBean respBean = teacherPostService.deleteTeacherPost(postId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
